/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package siii;

/**
 *
 * @author daniel
 */
// PDFViewer.java

import java.awt.*;
import java.awt.geom.*;

import java.io.*;

import java.nio.*;
import java.nio.channels.*;

import javax.swing.*;

import com.sun.pdfview.*;

public class PDFViewer extends JFrame
{
   static Image image;

   public PDFViewer (String title)
   {
      super (title);
      setDefaultCloseOperation (EXIT_ON_CLOSE);

      JLabel label = new JLabel (new ImageIcon (image));
      label.setVerticalAlignment (JLabel.TOP);

      setContentPane (new JScrollPane (label));

      pack ();
      setVisible (true);
   }

   public static void main (final String [] args) throws IOException
   {
     /* if (args.length < 1 || args.length > 2)
      {
          System.err.println ("usage: java PDFViewer pdfspec [pagenum]");
          return;
      }

      int pagenum = (args.length == 1) ? 1 : Integer.parseInt (args [1]);
      if (pagenum < 1)
          pagenum = 1;
*/
        int pagenum = 0;
      RandomAccessFile raf = new RandomAccessFile (new File ("gnutella_protocol_0.4.pdf"), "r");
      FileChannel fc = raf.getChannel ();
      ByteBuffer buf = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
      PDFFile pdfFile = new PDFFile (buf);

      int numpages = pdfFile.getNumPages ();
      System.out.println ("Number of pages = "+numpages);
      if (pagenum > numpages)
          pagenum = numpages;

      PDFPage page = pdfFile.getPage (pagenum);

      Rectangle2D r2d = page.getBBox ();

      double width = r2d.getWidth ();
      double height = r2d.getHeight ();
      width /= 72.0;
      height /= 72.0;
      int res = Toolkit.getDefaultToolkit ().getScreenResolution ();
      width *= res;
      height *= res;

      image = page.getImage ((int) width, (int) height, r2d, null, true, true);

      Runnable r = new Runnable ()
                   {
                       public void run ()
                       {
                          new PDFViewer ("PDF Viewer: ");
                       }
                   };
      EventQueue.invokeLater (r);
   }
}

